package com.fdd.furns.web;

import com.fdd.furns.pojo.Member;
import com.fdd.furns.service.MemberService;
import com.fdd.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {
    private MemberService memberService = new MemberServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("loginServlet 被访问了...");
        
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        Member member = new Member(0, username, password, null);
        Member loginMember = memberService.login(member);
        if (loginMember != null) {
            System.out.println("登录成功");
            req.getRequestDispatcher("views/member/login_ok.jsp").forward(req, resp);
        } else {
            System.out.println("登录失败");
            req.setAttribute("error","用户名或密码错误");
            //回显用户名
            req.setAttribute("username",username);
            //页面转发
            req.getRequestDispatcher("views/member/login.jsp").forward(req, resp);
        }

    }

}
